package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.account.enums.CodeEnum;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.SaleTypeDto;
import com.ruoyi.system.domain.vo.SaleTypeResultDto;
import com.ruoyi.system.domain.vo.SysConsumerDto;
import com.ruoyi.system.domain.vo.TemplateSaveDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.ISysConsumerService;

/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-27
 */
@Service
public class SysConsumerServiceImpl implements ISysConsumerService 
{
    @Autowired
    private SysConsumerMapper sysConsumerMapper;


    @Autowired
    private SysQualificationMaterialMapper sysQualificationMaterialMapper;

    @Autowired
    private SysReceiptInfoMapper sysReceiptInfoMapper;

    @Autowired
    private SysAuditLogMapper sysAuditLogMapper;

    /**
     * 查询用户信息
     * 
     * @param consumerId 用户信息主键
     * @return 用户信息
     */
    @Override
    public SysConsumer selectSysConsumerByUserId(Long consumerId)
    {
        return sysConsumerMapper.selectSysConsumerByConsumerId(consumerId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param sysConsumer 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysConsumer> selectSysConsumerList(SysConsumer sysConsumer)
    {
        return sysConsumerMapper.selectSysConsumerList(sysConsumer);
    }

    /**
     * 新增用户信息
     * 
     * @param sysConsumer 用户信息
     * @return 结果
     */
    @Override
    public int insertSysConsumer(SysConsumer sysConsumer)
    {
        return sysConsumerMapper.insertSysConsumer(sysConsumer);
    }

    /**
     * 修改用户信息
     * 
     * @param sysConsumer 用户信息
     * @return 结果
     */
    @Override
    public int updateSysConsumer(SysConsumer sysConsumer)
    {
        return sysConsumerMapper.updateSysConsumer(sysConsumer);
    }

    /**
     * 批量删除用户信息
     * 
     * @param consumerIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSysConsumerByUserIds(Long[] consumerIds)
    {
        return sysConsumerMapper.deleteSysConsumerByConsumerIds(consumerIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param consumerId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSysConsumerByUserId(Long consumerId)
    {
        return sysConsumerMapper.deleteSysConsumerByConsumerId(consumerId);
    }


    /**
     *
     * @param
     * @return
     */
    @Override
    public int operate(Long consumerId,String usageStatus) {
        SysConsumer sysConsumer = new SysConsumer();
        if(usageStatus.equals("1")){
            sysConsumer.setUsageStatus("2");
        }else{
            sysConsumer.setUsageStatus("1");
        }
        return sysConsumerMapper.updateSysConsumer(sysConsumer);
    }



    @Override
    public SysConsumerDto getDetailInfo(Long consumerId,String userType) {


        if(consumerId==null){
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "机构用户id不能为空!");
        }


        SysConsumerDto sysConsumerDto = new SysConsumerDto();

        /* 机构详情**/

        SysConsumer sysConsumer = sysConsumerMapper.selectSysConsumerByConsumerId(consumerId);
        List<SaleTypeDto> saleTypeDtoList = new ArrayList<>();
        SaleTypeDto saleTypeDto = new SaleTypeDto();
        SaleTypeDto saleTypeDto2 = new SaleTypeDto();
        String saleType = sysConsumer.getSaleType();
        if("1".equals(saleType)){

            saleTypeDto.setLabel("toB");
            saleTypeDto.setValue("1");
            saleTypeDto.setSelected("true");
            saleTypeDto2.setLabel("toC");
            saleTypeDto2.setValue("2");
            saleTypeDto2.setSelected("false");
        }
        if("2".equals(saleType)){
            saleTypeDto.setLabel("toB");
            saleTypeDto.setValue("1");
            saleTypeDto.setSelected("false");
            saleTypeDto2.setLabel("toC");
            saleTypeDto2.setValue("2");
            saleTypeDto2.setSelected("true");
        }
        if("3".equals(saleType)){
            saleTypeDto.setLabel("toB");
            saleTypeDto.setValue("1");
            saleTypeDto.setSelected("true");
            saleTypeDto2.setLabel("toC");
            saleTypeDto2.setValue("2");
            saleTypeDto2.setSelected("true");



        }
        saleTypeDtoList.add(saleTypeDto);
        saleTypeDtoList.add(saleTypeDto2);
        sysConsumer.setSaleTypeList(saleTypeDtoList);
        sysConsumerDto.setOrgInfo(sysConsumer);



        /* 收货信息 (采购商和个人才有收货信息)**/
        if(userType.equals("buyer")||userType.equals("cuser")){
            SysReceiptInfo sysReceiptInfo = new SysReceiptInfo();
            sysReceiptInfo.setUserId(consumerId);
            List<SysReceiptInfo> receiptInfoList = sysReceiptInfoMapper.selectSysReceiptInfoList(sysReceiptInfo);
            sysConsumerDto.setShippingInfo(receiptInfoList);

        }

        /* 资质材料**/
        SysQualificationMaterial sysQualificationMaterial = new SysQualificationMaterial();
        sysQualificationMaterial.setUserId(consumerId);
        List<SysQualificationMaterial> materialList = sysQualificationMaterialMapper.selectSysQualificationMaterialList(sysQualificationMaterial);
        sysConsumerDto.setQualificationMaterialsInfo(materialList);

        /* 审核日志**/
        SysAuditLog sysAuditLog = new SysAuditLog();
        sysAuditLog.setUserId(consumerId);
        List<SysAuditLog> auditLogList = sysAuditLogMapper.selectSysAuditLogList(sysAuditLog);
        sysConsumerDto.setAuditLogInfo(auditLogList);

        return sysConsumerDto;
    }

    /**
     *
     * @param saleTypeDtoList 销售方式参数对象
     * @return
     */
    @Override
    public int saleTypeSave(SaleTypeResultDto saleTypeDtoList) {

        List<SaleTypeDto> saleTypeDtos =saleTypeDtoList.getSaleTypeDtoList();
        String value = "";
        int i =0;
        String resultBValue = "";
        String resultCValue = "";
        for(SaleTypeDto saleTypeDto : saleTypeDtos){

            String selected = saleTypeDto.getSelected();
            String label = saleTypeDto.getLabel();

            if(selected.equals("true")&&label.equals("toB")){
                resultBValue = "1";
                i=i+1;
            }
            if(selected.equals("true")&&label.equals("toC")){
                resultCValue = "2";
                i=i+1;
            }

        }
        if(i==3){
            value = "3";
        }
        if(i==1){
            if(StringUtils.isEmpty(resultBValue)){
                value = resultCValue;
            }else{
                value = resultBValue;
            }
        }
        SysConsumer sysConsumer = new SysConsumer();
        sysConsumer.setSaleType(value);
        sysConsumer.setConsumerId(saleTypeDtoList.getConsumerId());
        return sysConsumerMapper.updateSysConsumer(sysConsumer);
    }

    /**
     *
     * @param templateSaveDto 模板修改参数对象
     * @return
     */
    @Override
    public int templateSave(TemplateSaveDto templateSaveDto) {
        SysConsumer sysConsumer = new SysConsumer();
        sysConsumer.setConsumerId(templateSaveDto.getConsumerId());
        //toB
        if(templateSaveDto.getSaleType().equals("1")) {
            sysConsumer.setBmodualId(templateSaveDto.getModualId());
            sysConsumer.setBmodualName(templateSaveDto.getModualName());
        }
        //toC
        if(templateSaveDto.getSaleType().equals("2")) {
            sysConsumer.setCmodualId(templateSaveDto.getModualId());
            sysConsumer.setCmodualName(templateSaveDto.getModualName());
        }

        return sysConsumerMapper.updateSysConsumer(sysConsumer);
    }
}
